package com.license.logic.ontology;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.AnonId;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.PrintUtil;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.Filter;
import com.hp.hpl.jena.vocabulary.RDF;

public class OntologyRationatorModel {
	
		Individual individual;
	    Resource   resource;
	    Statement  statement;
	    OntModel   domainModel;
		
		// create an ontology model with the language profile OWL full
		// storage model in-memory and reasoner : optimised rule-based reasoner with OWL rules
	/*	OntModelSpec ontModelSpec = new OntModelSpec(OntModelSpec.OWL_MEM_MINI_RULE_INF);
		OntDocumentManager docManager = new OntDocumentManager();
		ontModelSpec.setDocumentManager(docManager);
		OntModel model = ModelFactory.createOntologyModel(ontModelSpec);*/
		
/*		OntModel model = ModelFactory.createOntologyModel();
		OntDocumentManager documentManager = model.getDocumentManager();
		documentManager.addAltEntry("", "");*/
	    
	    private static final String VO_PREFIX="http://www.semanticweb.org/ontologies/2013/0/Ontology1357131527936.owl#";
	    private static final String MY_URI="urn:my:ontology";
	    final String CARDIO_OWL_URL="d:\\pt utcn\\workspace_licenta\\resources\\licentaRDFontology.owl";
	    Reasoner reasoner;
	    static final private Property hasChromosome=ModelFactory.createDefaultModel().createProperty(MY_URI,"chromosome");
	    static final private Property hasPosition=ModelFactory.createDefaultModel().createProperty(MY_URI,"position");
	    
	    public OntologyRationatorModel() throws IOException
	      {
	      OntModel ontModel = ModelFactory.createOntologyModel();
	       InputStream in = FileManager.get().open(CARDIO_OWL_URL);
	      ontModel.read(in, "");
	      in.close();
	      this.reasoner = ReasonerRegistry.getOWLReasoner();
	      this.reasoner=this.reasoner.bindSchema(ontModel);
	      }
	    
	    public void run()
	      {
	      Random rand=new Random();
	      com.hp.hpl.jena.rdf.model.Model instances = ModelFactory.createDefaultModel();
	      instances.setNsPrefix("vo",VO_PREFIX);
	      instances.setNsPrefix("my",MY_URI);

	      for(int i=0;i< 10;++i)
	        {
	        Resource subject= null;
	        Resource rdftype=null;
	        switch(i%3)
	          {
	          case 0:
	            {
	            //modified amino acid
	            subject=instances.createResource(AnonId.create("modaa_"+i));
	            rdftype=instances.createResource(VO_PREFIX+"VariO_0000029");
	            break;
	            }
	          case 1:
	            {
	            subject=instances.createResource(AnonId.create("spliced_"+i));
	            rdftype=instances.createResource(VO_PREFIX+"VariO_0000030");
	            break;
	            }
	          default:
	            {
	            //effect on protein subcellular localization
	            subject=instances.createResource(AnonId.create("subcell_"+i));
	            rdftype=instances.createResource(VO_PREFIX+"VariO_0000033");
	            break;
	            }
	          }
	        instances.add(subject, RDF.type, rdftype);
	        instances.add(subject, hasChromosome, instances.createLiteral("chr"+(1+rand.nextInt(22))));
	        instances.add(subject, hasPosition, instances.createTypedLiteral(rand.nextInt(1000000)));
	        }
	      
	      InfModel model = ModelFactory.createInfModel (reasoner, instances);
	      ExtendedIterator<Statement> sti = model.listStatements(null, null, model.createResource(VO_PREFIX+"VariO_0000028"));
	      sti=sti.filterKeep(new Filter<Statement>()
	          {
	          @Override
	          public boolean accept(Statement stmt)
	            {
	            return  stmt.getSubject().getProperty(hasChromosome)!=null &&
	                stmt.getSubject().getProperty(hasPosition)!=null
	                ;
	            }
	          });
	      while(sti.hasNext() )
	        {
	        Statement stmt = sti.next();
	        System.out.println("\t+ " + PrintUtil.print(stmt));
	        Statement val=stmt.getSubject().getProperty(hasChromosome);
	        System.out.println("\t\tChromosome:\t"+val.getObject());
	        val=stmt.getSubject().getProperty(hasPosition);
	        System.out.println("\t\tPosition:\t"+val.getObject());
	        }
	      }
	    
	    public static void main(String[] args) throws Exception
	      {
	        OntologyRationatorModel app=new OntologyRationatorModel();
		      app.run();
		      System.out.println("EEENNNDDD_____________________---------");
	    }
	  }


